﻿using Common.Models;
using Core.Entities.Stock;
using Infrastructure.Services;
using Infrastructure.Services.SM;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace WebZHX.Controllers.SM
{
    /// <summary>
    /// 入库记录控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class StockInController : BaseController<StockIn, string>
    {
        private readonly StockInService _stockInService;

        public StockInController(
            BaseService<StockIn, string> service,
            StockInService stockInService)
            : base(service)
        {
            _stockInService = stockInService;
        }

        /// <summary>
        /// 根据物料编码查询入库记录
        /// </summary>
        /// <param name="materCode">物料编码</param>
        /// <returns>入库记录列表</returns>
        [HttpGet("mater/{materCode}")]
        public async Task<ActionResult<IEnumerable<StockIn>>> GetByMaterCode(string materCode)
        {
            var result = await _stockInService.GetByMaterCodeAsync(materCode);
            return Ok(result);
        }

        /// <summary>
        /// 根据批次号查询入库记录
        /// </summary>
        /// <param name="batchCode">批次号</param>
        /// <returns>入库记录列表</returns>
        [HttpGet("batch/{batchCode}")]
        public async Task<ActionResult<IEnumerable<StockIn>>> GetByBatchCode(string batchCode)
        {
            var result = await _stockInService.GetByBatchCodeAsync(batchCode);
            return Ok(result);
        }

        /// <summary>
        /// 根据仓库ID查询入库记录
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>入库记录列表</returns>
        [HttpGet("warehouse/{warehouseId}")]
        public async Task<ActionResult<IEnumerable<StockIn>>> GetByWarehouseId(string warehouseId)
        {
            var result = await _stockInService.GetByWarehouseIdAsync(warehouseId);
            return Ok(result);
        }

        /// <summary>
        /// 根据筒仓编码查询入库记录
        /// </summary>
        /// <param name="siloCode">筒仓编码</param>
        /// <returns>入库记录列表</returns>
        [HttpGet("silo/{siloCode}")]
        public async Task<ActionResult<IEnumerable<StockIn>>> GetBySiloCode(string siloCode)
        {
            var result = await _stockInService.GetBySiloCodeAsync(siloCode);
            return Ok(result);
        }

        /// <summary>
        /// 高级分页查询入库记录
        /// </summary>
        /// <param name="dto">查询参数</param>
        /// <returns>分页结果</returns>
        [HttpPost("Page")]
        public async Task<ActionResult<PagedResult<StockIn>>> AdvancedPagedQuery([FromBody] StockInService.StockInQueryDto dto)
        {
            var result = await _stockInService.AdvancedPagedQueryAsync(dto.Filter, dto.StockIn);
            return Ok(result);
        }

        /// <summary>
        /// 保存入库记录
        /// </summary>
        /// <param name="model">入库记录</param>
        /// <returns>操作结果</returns>
        [HttpPost("save")]
        public async Task<ActionResult<StockIn>> SaveStockIn([FromBody] StockIn model)
        {
            // 验证必填字段
            if (string.IsNullOrEmpty(model.MaterCode))
            {
                return BadRequest("物料编码不能为空");
            }

            if (string.IsNullOrEmpty(model.MaterName))
            {
                return BadRequest("物料名称不能为空");
            }

            if (model.In_Quantity <= 0)
            {
                return BadRequest("入库数量必须大于0");
            }

            if (string.IsNullOrEmpty(model.Id))
            {
                // 新增
                model.Id = Guid.NewGuid().ToString();
                model.In_Time = DateTime.Now;
                var result = await _stockInService.CreateAsync(model);
                return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
            }
            else
            {
                // 修改
                var result = await _stockInService.UpdateAsync(model);
                return Ok(result);
            }
        }

        /// <summary>
        /// 软删除入库记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("delete/{id}")]
        public async Task<ActionResult> SoftDelete(string id)
        {
            await _stockInService.SoftDeleteAsync(id);
            return Ok("删除成功");
        }
    }
}